package org.sgx.gwteditors.testgallery.gallery;

import java.util.Map;

import org.sgx.gwteditors.client.editor.props.PropertyHaver;
import org.sgx.gwteditors.client.editor.props.PropertyHaverUtil;
import org.sgx.gwteditors.client.editor.props.PropertyInfo;
import org.sgx.gwteditors.client.impl1.EditorFramework1;
import org.sgx.gwteditors.client.impl1.complex.Array1DEditor;
import org.sgx.gwteditors.client.impl1.complex.PropertyHaverEditor1;
import org.sgx.gwteditors.client.impl1.edbean.EditableBean;
import org.sgx.gwteditors.client.util.LWOMap;
import org.sgx.gwteditors.client.util.TestUtil;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.resources.client.ExternalTextResource;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;
/**
 * 
 * @author sg
 *
 */
public class Array1DEditorTest1 implements Test {

	/** 
	 * The java bean we want to edit. 
	 * @author sg
	 */
	public static abstract class Person implements EditableBean {	
		
		String name;
		Integer age;
		Boolean male;
		public static Person create(String name, Integer age, Boolean male) {
			Person p = GWT.create(Person.class); 
			p.setName(name); 
			p.setAge(age); 
			p.setMale(male); 
			return p;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public Integer getAge() {
			return age;
		}
		public void setAge(Integer age) {
			this.age = age;
		}
		public Boolean getMale() {
			return male;
		}
		public void setMale(Boolean male) {
			this.male = male;
		}	
	}
	
	private Array1DEditor<Person> ed1;	
	
	@Override
	public void test(Panel parent) {
		new EditorFramework1().start();
		
		VerticalPanel vp = new VerticalPanel();
		vp.setWidth("100%");
		parent.add(vp);	//make sure the editor parent (vp) is attached to the dom before using the editor
		
		//now let's edit an 1d array of persons
		
		Person[] persons = new Person[]{
			Person.create("sebastián", 28, true),
			Person.create("laura", 23, false),
			Person.create("andres", 16, true)
		};
		
		ed1 = new Array1DEditor<Person>(EditorFramework1.TYPE_PROPERTYHAVER, false);
		ed1.load(persons);
		ed1.setWidth("100%");
		vp.add(ed1);	
		
		Button b1 = new Button("edit!", new ClickHandler() {			
			@Override
			public void onClick(ClickEvent event) {
				Person[] editedPersons = ed1.flush();				
				Window.alert("first edited name : "+editedPersons[0].getName());
			}
		});		
		vp.add(b1);
	}

	
	//test stuff
	@Override
	public String getName() {
		return "array editor 1";
	}

	@Override
	public Map<String, ExternalTextResource> getJavaSources() {
		return TestUtil.getResource(getName(), TestResources.INSTANCE.Array1DEditorTest1());
	}
//	@Override
//	public String[] getJavaSources() {
//		return new String[]{TestResources.INSTANCE.Array1DEditorTest1().getText()};
//	}
	@Override
	public String[] getTags() {
		return new String[]{};
	}
	@Override
	public String getDescription() {
		return "1d and 2d array editors of complex and simple types. ";
	}
}
